package smartclass.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import smartclass.data.ManagerDataBaseHelper;
import smartclass.data.FinalValues;
import smartclass.entity.TestInfo;
import smartclass.present.LogInActivity;

public class TestDataDao {
	static TestDataDao testDao;
	ManagerDataBaseHelper db;
	String tableName;

	public TestDataDao() {
		db = LogInActivity.db;
		tableName = ManagerDataBaseHelper.TEST_TABLE;
	}

	public static TestDataDao getInstance() {
		if (testDao == null) {
			testDao = new TestDataDao();
		}
		return testDao;
	}

	// Add Item
	public boolean addTest(TestInfo test) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");

		ContentValues value = new ContentValues();
		value.put(FinalValues.NAME, test.getName());
		value.put(FinalValues.DATE_CREAT, test.getDateCreate());
		value.put(FinalValues.TYPE, test.getType());
		value.put(FinalValues.PATH, test.getPath());
		value.put(FinalValues.ID_GIAO_VIEN, test.getIdTeacher());
		data.insert(tableName, null, value);
		data.close();
		return true;
	}

	// Get Item
	public TestInfo getTest(int id) {
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		TestInfo testInfo = new TestInfo();
		Cursor c = null;

		try {
			String[] params = new String[] { String.valueOf(id) };
			String sql = "SELECT * FROM " + tableName + " WHERE "
					+ FinalValues.ID + "=?";
			c = data.rawQuery(sql, params);
			c.moveToFirst();
			testInfo.setId(Integer.parseInt(c.getString(0)));
			testInfo.setName(c.getString(1));
			testInfo.setDateCreate(c.getString(2));
			testInfo.setType(c.getString(3));
			testInfo.setPath(c.getString(4));
			testInfo.setIdTeacher(Integer.parseInt(c.getString(5)));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return testInfo;
	}

	// Get All Item
	public ArrayList<TestInfo> getAllTest() {
		ArrayList<TestInfo> allTest = new ArrayList<TestInfo>();
		Cursor c = null;
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		String sql = "SELECT * FROM " + tableName;
		c = db.getReadableDatabase().rawQuery(sql, null);
		TestInfo testInfo = null;
		if (c.moveToFirst()) {
			do {
				testInfo = new TestInfo();
				testInfo.setId(Integer.parseInt(c.getString(0)));
				testInfo.setName(c.getString(1));
				testInfo.setDateCreate(c.getString(2));
				testInfo.setType(c.getString(3));
				testInfo.setPath(c.getString(4));
				testInfo.setIdTeacher(Integer.parseInt(c.getString(5)));
			} while (c.moveToNext());
		}
		return allTest;
	}

	// Edit Item

	public boolean updateTest(TestInfo test) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		ContentValues value = new ContentValues();
		value.put(FinalValues.NAME, test.getName());
		value.put(FinalValues.DATE_CREAT, test.getDateCreate());
		value.put(FinalValues.TYPE, test.getType());
		value.put(FinalValues.PATH, test.getPath());
		value.put(FinalValues.ID_GIAO_VIEN, test.getIdTeacher());
		data.update(tableName, value, FinalValues.ID + "=?",
				new String[] { String.valueOf(test.getId()) });
		data.close();
		return true;
	}

	// Delete Item

	public boolean deleteDirection(int id) {
		String sql = "DELETE FROM " + tableName + " WHERE " + FinalValues.ID
				+ "=" + id;
		db.getWritableDatabase().execSQL(sql);
		return true;
	}

}
